<?php
/************************
 *  Codenova.pl Project
 *  Created by: Kamil Mrzygłód
 *
 *  (c) 2013 by Codenova.pl
 *  All Right Reserved
 ***********************/

namespace Modules\GotchaAdmin\Controllers;

use Gotcha\App\Framework\Controller;
use GotchaDatabase\GotchaLayersQuery;
use GotchaDatabase\GotchaZonesQuery;

class LayoutController extends Controller {
    public function Index() {
        return parent::View();
    }

    public function CreateLayer() {
        return parent::View();
    }

    public function EditLayer() {
        return parent::View();
    }

    public function DeleteLayer() {
        return parent::View();
    }

    public function AddZone() {
        return parent::View();
    }

    public function returnLayers() {
        $layers = GotchaLayersQuery::create()
                    ->orderById()
                        ->find();

        // String builder
        $html = '';
        foreach ($layers as $layer) {
            $html .= '<tr>';
            $html .= sprintf('<td>%s</td><td>%s</td><td>[ <a href="%s?id=%s">Edytuj</a> | <a href="%s?id=%s" class="delete">Usuń</a> ]</td>', $layer->getStrongName(), $layer->getAlias(), parent::UrlContent('/Admin/Layout/EditLayer'), $layer->getId(), parent::UrlContent('/Admin/Layout/DeleteLayer'), $layer->getId());
            $html .= '</tr>';
        }

        return $html;
    }

    public function returnZones() {
        $layers = GotchaLayersQuery::create()
            ->orderById()
            ->find();

        $html = '';

        foreach ($layers as $layer) {
            $html .= '<tr class="layerName"><td colspan="4">'.$layer->getStrongName().' [ <a href="'.parent::UrlContent('/Admin/Layout/AddZone').'?id='.$layer->getId().'">Dodaj nową strefę</a> ]</td></tr>';

            $zones = GotchaZonesQuery::create()
                        ->filterByGotchaLayersId($layer->getId())
                            ->orderById()
                                ->find();

            // String builder
            foreach ($zones as $zone) {
                $html .= '<tr>';
                $html .= sprintf('<td>%s</td><td>%s</td><td>%s</td><td>[ <a href="%s?id=%s">Edytuj</a> | <a href="%s?id=%s" class="delete">Usuń</a> ]</td>', $zone->getStrongName(), $zone->getContentType(), $zone->getContentStrongName(), parent::UrlContent('/Admin/Layout/EditZone'), $zone->getId(), parent::UrlContent('/Admin/Layout/DeleteZone'), $zone->getId());
                $html .= '</tr>';
            }
        }

        return $html;
    }
}